<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 15:23
 */

namespace app\admin\controller;

//会员管理
use app\Model\RegionModel;
use app\Model\UserAccountLogModel;
use app\Model\UserAddressModel;
use app\Model\UserLeverModel;
use app\Model\UsersModel;
use think\Collection;

class Member extends Common
{
    /*
     * 会员列表页面
     */
    public function index()
    {
        $postData = input('get.');
        $where = screen($postData,['u.mobile'=>'mobile','u_l.level_id'=>'level_id']);
        $getUser = UsersModel::alias('u')
            ->join('tp_user_level u_l','u.level=u_l.level_id')//会员等级表
            ->where('is_del','=',0)//0为未删除
            ->where($where) //搜索筛选条件
            ->order('u.user_id','desc')
            ->paginate(null,false,['query'=>request()->param()]);


        $getUserxx = UsersModel::where('is_del','=',0)//0为未删除
            ->select();
        $getUsertoarray = $getUserxx->toArray();

//        dump($getUsertoarray);die();
        {
            $lvData1 = [];//下级数（1级）
            $lvData2 = [];//下级数（2级）
            $lvData3 = [];//下级数（3级）
    //        dump($getUsertoarray);die();
            foreach ($getUsertoarray as $k=>$v){
                if ($v['first_leader']!=0){//没有用户0
                    array_push($lvData1,$v['first_leader']);
                    array_push($lvData2,$v['second_leader']);
                }
            }
            $first= array_count_values($lvData1);//统计个数组中值出现的次数
            foreach ($getUsertoarray  as $k=>$v){//没有的设置0
                if (!isset($first[$v['user_id']])){
                    $first[$v['user_id']]=0;
                }
            }
            $second= array_count_values($lvData2);//统计个数组中值出现的次数
            foreach ($getUsertoarray  as $k=>$v){
                if (!isset($second[$v['user_id']])){
                    $second[$v['user_id']]=0;
                }
            }

            $third= array_count_values($lvData3);//统计个数组中值出现的次数
            foreach ($getUsertoarray  as $k=>$v){
                if (!isset($third[$v['user_id']])){
                    $third[$v['user_id']]=0;
                }
            }
            session('first',$first);
            session('second',$second);
            session('third',$third);
            $data['first']=$first;
            $data['second']=$second;
            $data['third']=$third;
        }
        $getUserLever = UserLeverModel::select();
        $data['lever']=$getUserLever;
        $data['data']=$getUser;
        $this->getConAndAct('member');
        return view('index',$data);
    }

    /*
     * 会员（用户）新增/编辑页面
     */
    public function add()
    {
        $getData=input('get.');
        //会员等级
        $data['level']=UserLeverModel::column('level_name','level_id');

        if ($getData){
            $getUserFind = UsersModel::find($getData['user_id']);
            $data['data']=$getUserFind;
            //统计下线
            $data2['first']=session('first')[$getData['user_id']];
            $data2['second']=session('second')[$getData['user_id']];
            $data2['third']=session('third')[$getData['user_id']];

            $data['XX']=$data2;
//            dump($data2);die();

            return view('edit',$data);//编辑页
        }
        return view('add',$data);//新增页
    }

    /*
     * 会员（用户）新增/编辑
     */
    public function addData()
    {
        $postData=input('post.');
        $postData['password']=encryption(input('post.password'));
        if (isset($postData['user_id'])){
            if (input('post.password')=='0000'){
                unset($postData['password']);//密码不改动
            }
            sole('tp_users',['mobile'=>$postData['mobile'],'is_del'=>0],['user_id',$postData['user_id']],'手机号');//编辑判断手机是否存在
            $setUser = UsersModel::update($postData,['user_id'=>$postData['user_id']]);

        }else{
            $postData['reg_time']=time();//加入时间
            sole('tp_users',['mobile'=>$postData['mobile'],'is_del'=>0],false,'手机号');//判断手机是否存在
            $setUser = UsersModel::insert($postData);
        }
        return setDatabase($setUser);
    }

    /*
     * 会员删除（软删除）
     */
    public function del()
    {
        $postData = input('post.');
        $delUser = UsersModel::update(['is_del'=>time()],['user_id'=>$postData['id']]);//软删除用户
        //需要吧上级是该用户的会员清除上级编号
        $where=[
            'first_leader'=>$postData['id'],
            'second_leader'=>$postData['id'],
            'third_leader'=>$postData['id'],
        ];
//        会员删除需要清除其下线的联系（软删除的也清除）
        setDatabaseDef($delUser,'软删除失败');
        $setUser1 = UsersModel::where('first_leader','=',$postData['id'])->update(['first_leader'=>0]);
        $setUser2= UsersModel::where('second_leader','=',$postData['id'])->update(['second_leader'=>0]);
        return retJson('',200,'第一下线'.$setUser1.'条，第二下线'.$setUser2.'条，释放成功');
    }
    /*
     * 收货地址
     */
    public function toHome()
    {
        $getData = input('get.');
        $data['data'] = UserAddressModel::where(['user_id'=>$getData['user_id']])->select();
        return view('toHome',$data);
    }
    
    /*
     * 账户资金记录列表
     */
    public function accountLog()
    {
        $getData = input('get.');
        $getUserAccount =UserAccountLogModel::where(['user_id'=>$getData['user_id']])
            ->paginate('',false,['query'=>['user_id'=>$getData['user_id']]]);

        $data['data']=$getUserAccount;
        return view('accountLog',$data);
    }
}